package com.smartwasser.yunhtgl.apphtgl.asps;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.listener.DialogUIItemListener;
import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.activity.ImageLookActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.bean.BaseBean;
import com.smartwasser.yunhtgl.apphtgl.bean.FlMessageBean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat1Bean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat5Bean;
import com.smartwasser.yunhtgl.apphtgl.function.OfficeReader;
import com.smartwasser.yunhtgl.apphtgl.photo.ImagePagerActivity;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPNormal11PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPNormal1Presenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPNormal1PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.utils.DialogUI;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.view.SPNormal11View;
import com.smartwasser.yunhtgl.apphtgl.view.SPNormal1View;
import com.smartwasser.yunhtgl.apphtgl.widget.QianzigaizhangView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.dou361.dialogui.DialogUIUtils.showAlertHorizontal;
import static com.dou361.dialogui.DialogUIUtils.showBottomSheetAndCancel;
import static com.smartwasser.yunhtgl.apphtgl.base.BaseConstant.successSPGreat1SH;
import static com.smartwasser.yunhtgl.apphtgl.network.Retrofits.base;
import static com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils.isBCVisible;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getPath;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getQicaoType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getSZType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getType;

/**
 * 待完成合同详情接口
 * 个人未处理
 * 用印合同
 * 归档审核中
 */
public class SPCase11Activity extends BaseActivity implements SPNormal11View {
    private SPNormal1Presenter sp1CasePresenter = new SPNormal11PresenterImpl(this);
    private SPGreat5Bean type1Sp5CaseBean;
    private Dialog loadingHorizontal;
    private FlMessageBean mflMessageBean;
    private String fl_id;
    private String id;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.sp1_normal_scroll)
    ScrollView sp1_normal_scroll;
    @BindView(R.id.sp1_normal_name)
    TextView sp1_normal_name;//合同名称
    @BindView(R.id.sp1_normal_code)
    TextView sp1_normal_code;//合同编号
    @BindView(R.id.sp1_great_sq)
    TextView sp1_great_sq;//合同申请部室
    @BindView(R.id.sp1_great_zgy)
    TextView sp1_great_zgy;//合同专管员
    @BindView(R.id.sp1_great_creat_time)
    TextView sp1_great_creat_time;//合同申请时间
    @BindView(R.id.sp1_great_fen)
    TextView sp1_great_fen;//合同份数
    @BindView(R.id.sp1_normal_main)
    TextView sp1_normal_main;//授权签署人
    @BindView(R.id.sp1_normal_yj)
    TextView sp1_normal_yj;//单位负责人意见
    @BindView(R.id.sp1_normal_fg)
    TextView sp1_normal_fg;//集团分管领导
    @BindView(R.id.sp1_normal_fg_yj)
    TextView sp1_normal_fg_yj;//集团分管领导意见
    @BindView(R.id.sp1_great_bz)
    TextView sp1_great_bz;//备注
    @BindView(R.id.sp1_great_qc)
    TextView sp1_great_qc;//起草
    @BindView(R.id.sp1_great_zw)
    TextView sp1_great_zw;//正文
    @BindView(R.id.sp1_great_yj)
    TextView sp1_great_yj;//法律意见书
    @BindView(R.id.sp1_great_sz)
    TextView sp1_great_sz;//收支情况
    @BindView(R.id.sp1_great_lx)
    TextView sp1_great_lx;//合同类型
    @BindView(R.id.sp1_great_qz)
    TextView sp1_great_qz;//签字盖章
    @BindView(R.id.sp1_great_feed)
    EditText sp1_great_feed;//在此处进行意见录入
    @BindView(R.id.sp1_great_allow)
    TextView sp1_great_allow;//通不通过
    @BindView(R.id.sp1_great_check)
    CheckBox sp1_great_check;//按钮
    @BindView(R.id.sp1_great_fl_all)
    LinearLayout sp1_great_fl_all;//补充信息可不可见
    private ArrayList<String> imageUrls = new ArrayList<>();

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_sp_case1);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("用印合同审核");
        sp1_normal_scroll.setVisibility(View.INVISIBLE);
        id = getIntent().getStringExtra("id");
        sp1_great_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean boom) {
                if (boom) {
                    sp1_great_allow.setText("不通过");
                    sp1_great_feed.setText("");
                } else {
                    sp1_great_allow.setText("通过");
                    sp1_great_feed.setText("通过");
                }
            }
        });
    }

    @Override
    public void initData() {
        loadingHorizontal = showLoadingHorizontal(this, "加载中...").show();
        try {
            sp1CasePresenter.getType1Sp1Normal(getStringData(this, "JSESSIONID", ""), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.back, R.id.sp1_normal_sh, R.id.sp1_great_fl_chose, R.id.sp1_great_zw_all})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sp1_normal_sh:
                if (sp1_great_feed.getText().toString().trim().length() > 0) {
                    Logger.e("R.id.sp1_great_sh");
                    commitSH();
                } else {
                    showResult("请输入审核意见");
                }
                break;
            case R.id.sp1_great_fl_chose:
                if (null != mflMessageBean && mflMessageBean.getFlyjsList().size() > 0) {
                    commitShow(mflMessageBean);
                } else {
                    loadingHorizontal = showLoadingHorizontal(this, "请稍等...").show();
                    sp1CasePresenter.getFlMessage(getStringData(this, "JSESSIONID", ""));
                }
                break;
            case R.id.sp1_great_zw_all:
                if (null != type1Sp5CaseBean.getBuContractBaseInfo()) {
                    String path = type1Sp5CaseBean.getBuContractBaseInfo().getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
        }
    }

    @Override
    public void success(SPGreat5Bean tb) {
        if (null != tb && null != tb.getBuContractBaseInfo()) {
            this.type1Sp5CaseBean = tb;
            sp1_normal_code.setText(tb.getBuContractBaseInfo().getCode());
            sp1_normal_name.setText(tb.getBuContractBaseInfo().getName());
            sp1_great_lx.setText(getType(tb.getBuContractBaseInfo().getType()));
            sp1_great_qc.setText(getQicaoType(tb.getBuContractBaseInfo().getQicaoType()));
            sp1_great_zw.setText(getPath(tb.getBuContractBaseInfo().getPath()));
            sp1_great_sz.setText(getSZType(tb.getBuContractBaseInfo().getShouzhiType()));
            if (null != tb.getBuContractBaseInfo().getBuMajorContractSignInfoList() && tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().size() > 0) {
                sp1_great_fen.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractCount() + "份");
                sp1_great_sq.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDept().getName());
                sp1_great_zgy.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractOfficer().getName());
                sp1_great_creat_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDate());
                sp1_great_bz.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getRemarks());
                sp1_normal_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractOfficer().getName());
                sp1_normal_yj.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractDwOfficerOpinion());
                sp1_normal_fg.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeLeader().getName());
                sp1_normal_fg_yj.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeOpinion());
            }
            if (isBCVisible(tb.getBuContractBaseInfo().getGdAct().getTaskDefKey())) {
                sp1_great_fl_all.setVisibility(View.VISIBLE);
                String qzht = tb.getBuContractBaseInfo().getQianziContract();
                if (!qzht.isEmpty() && qzht.length() > 0) {
                    final String[] qiht=qzht.split("\\|");
                    for(int i=1;i<qiht.length;i++){
//                        imageUrls.add(base+qiht[i]);
                        imageUrls.add("https://wxt.sinaimg.cn/thumb300/4daece65ly1ftw7tgy0cvj21ba0qowo7.jpg?tags=%5B%5D");
                        QianzigaizhangView qianzigaizhangView=new QianzigaizhangView(SPCase11Activity.this,null);
                        String[] qzgz_array = qiht[i].split("/");
                        qianzigaizhangView.setText(qzgz_array[qzgz_array.length-1]);
                        if (i==1)
                            qianzigaizhangView.setVisibilityTitle();
                        sp1_great_fl_all.addView(qianzigaizhangView);
                        final int finalI = i;
                        qianzigaizhangView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (null != type1Sp5CaseBean.getBuContractBaseInfo()) {
                                    String path = qiht[finalI];
                                    if (!path.isEmpty() && path.length() > 0) {
                                        if(path.contains(".JPG")||path.contains(".PNG")||path.contains(".jpg")||path.contains(".png")){
//                                            startActivity(ImageLookActivity.class, "url", path);
                                            imageBrower(finalI-1);
                                        }else{
                                            startActivity(OfficeReader.class, "url", path);
                                        }
                                    } else {
                                        showResult("暂无数据");
                                    }
                                } else {
                                    showResult("暂无数据");
                                }
                            }
                        });
                    }
                }
            } else {
                sp1_great_fl_all.setVisibility(View.GONE);
            }
        }
        sp1_normal_scroll.setVisibility(View.VISIBLE);
        dismiss(loadingHorizontal);
    }
    private void imageBrower(int position) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, imageUrls);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        startActivity(intent);
    }

    @Override
    public void successGetFlMessage(FlMessageBean flMessageBean) {
        this.mflMessageBean = flMessageBean;
        dismiss(loadingHorizontal);
        commitShow(flMessageBean);
    }

    @Override
    public void successCommitSH(BaseBean baseBean) {
        dismiss(loadingHorizontal);
        showResult("审核成功");
        setResult(successSPGreat1SH);
        finish();
    }

    @Override
    public void error() {
        dismiss(loadingHorizontal);
        showError();
    }

    public void commitShow(final FlMessageBean flMessageBean) {
        final List<String> strings = new ArrayList<>();
        for (int i = 0; i < flMessageBean.getFlyjsList().size(); i++) {
            strings.add(flMessageBean.getFlyjsList().get(i).getName());
        }
        showBottomSheetAndCancel(this, strings, "取消", new DialogUIItemListener() {
            @Override
            public void onItemClick(CharSequence text, int position) {
                sp1_great_yj.setText(flMessageBean.getFlyjsList().get(position).getName());
                fl_id = flMessageBean.getFlyjsList().get(position).getId();
            }
        }).show();
    }

    public void commitSH() {
        final String flag;
        final String comment = sp1_great_feed.getText().toString().trim();
        final String shouzhiType = type1Sp5CaseBean.getBuContractBaseInfo().getShouzhiType();
        final String type = type1Sp5CaseBean.getBuContractBaseInfo().getType();
        if (sp1_great_check.isChecked()) {
            flag = "1";
        } else {
            flag = "0";
        }
        Logger.e("JSESSIONID = " + getStringData(SPCase11Activity.this, "JSESSIONID", ""));
        Logger.e("id = " + id);
        Logger.e("flag = " + flag);
        Logger.e("comment " + comment);
        Logger.e("shouzhiType " + shouzhiType);
        Logger.e("type = " + type);
        showAlertHorizontal(this, "审核提示", "确定要审核吗？", new DialogUI() {
            @Override
            public void onPositive() {
                try {
                    if (null != fl_id && !fl_id.isEmpty() && fl_id.length() > 0) {
                        sp1CasePresenter.getCommitSH(getStringData(SPCase11Activity.this, "JSESSIONID", ""), id, flag, comment, shouzhiType, type, fl_id);
                    } else {
                        sp1CasePresenter.getCommitSH(getStringData(SPCase11Activity.this, "JSESSIONID", ""), id, flag, comment, shouzhiType, type, null);
                    }
                } catch (Exception e) {
                    Logger.e("请求网络大法启动失败");
                }
            }
        }).show();
    }
}